<?php

$user_id = $path[3];

if (ctype_digit($user_id)) {
	
	$user = new User($user_id);
	if ($_POST) {
		
		if (!isset($_SESSION['login']['user']) or $_SESSION['login']['user']['user_type'] != 2) {
			unset($_POST['core']['user_type'], $_POST['core']['access_rights']);
		}
		
		// make sure these are arrays if they're not already
		if (!isset($user->user['custom']) or !is_array($user->user['custom'])) $user->user['custom'] = array();
		if (!isset($_POST['custom']) or !is_array($_POST['custom'])) $_POST['custom'] = array();
		
		$user->user['core'] = array_merge($user->user['core'], $_POST['core']);
		$user->user['custom'] = array_merge($user->user['custom'], $_POST['custom']);
		if ($_POST['password1']) {
			$user->user['password1'] = $_POST['password1'];
			$user->user['password2'] = $_POST['password2'];
		}
		if ($user->save($errors)) {
			// all ok
			$smarty->assign('success', true);
		} else {
			$smarty->assign('errors', $errors);
		}
	}
	$smarty->assign('user', $user->user);
	$smarty->assign('users_custom_schema', $user->users_custom_schema);
	
	if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) and $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') {
		header('Content-type: text/xml');
		$smarty->display('global/users/_edit_messages.tpl.taconite');
	} else {
		$smarty->display('global/users/edit.tpl.html');
	}	
} else {
	$_SESSION['messages'][] = array('title' => 'Error', 'body' => "No user ID was provided.");
	header("Location: /admin/global/users/");
}
